<?
if(!isset($_SESSION['web_visited'])) {
	$_SESSION['web_visited'] = true;
	addVisitor();
}

$user = new User("", "", ROLE_PUBLIC);

if(isset($_SESSION['logged_user'])) {
	$user = $_SESSION['logged_user'];
	if(authorized($_SESSION['auth_code'], $user->getId())) {
		$_SESSION['auth_code'] = newAuthCode($user->getId());	
	} else {
		$user = new User("", "", ROLE_PUBLIC);
	}
} else if (isset($_COOKIE['cookname']) && isset($_COOKIE['cookpass']) && userLogin($_COOKIE['cookname'], $_COOKIE['cookpass'])){
	$user = getUserByUsername($_COOKIE['cookname']);
	$_SESSION['logged_user'] = $user;
	$_SESSION['auth_code'] = newAuthCode($user->getId());
	setcookie("cookname", $_COOKIE['cookname'], time() + LOGIN_REMEMBER_TIME, "/");
	setcookie("cookpass", $_COOKIE['cookpass'], time() + LOGIN_REMEMBER_TIME, "/");
}

$action = $_GET['action'];

if(!isset($_GET['page'])) {
      $page = PAGE_NEWS;
} else {      
      $page = $_GET['page'];
      if (is_numeric($page) && $page < count($pages)){
        $page = number_format($page);
      } else {
        $page = PAGE_NOT_FOUND;
      }    
}

if($user->getRole() < $pages[$page]["access"] || $user->getRole() < $actions[$action]["access"]) {
	$page = PAGE_NO_ACCESS;
	unset($action);
}
?>
